home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / idebug.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.9 KB  |  320 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: idebug.c,v 1.2 2000/09/19 19:00:43 lpd Exp $ */
  20. /* Debugging support for Ghostscript interpreter */
  21. /* This file must always be compiled with DEBUG set. */
  22. #undef DEBUG
  23. #define DEBUG
  24. #include "string_.h"
  25. #include "ghost.h"
  26. #include "gxalloc.h"        /* for procs for getting struct type */
  27. #include "idebug.h"        /* defines interface */
  28. #include "idict.h"
  29. #include "iname.h"
  30. #include "ipacked.h"
  31. #include "istack.h"
  32. #include "iutil.h"
  33. #include "ivmspace.h"
  34. #include "opdef.h"
  35.  
  36. /* Table of type name strings */
  37. static const char *const type_strings[] = {
  38.     REF_TYPE_DEBUG_PRINT_STRINGS
  39. };
  40.  
  41. /* First unassigned type index */
  42. extern const int tx_next_index;    /* in interp.c */
  43.  
  44. /* Print a name. */
  45. void
  46. debug_print_name(const ref * pnref)
  47. {
  48.     ref sref;
  49.  
  50.     name_string_ref(pnref, &sref);
  51.     debug_print_string(sref.value.const_bytes, r_size(&sref));
  52. }
  53. void
  54. debug_print_name_index(name_index_t nidx)
  55. {
  56.     ref nref;
  57.  
  58.     name_index_ref(nidx, &nref);
  59.     debug_print_name(&nref);
  60. }
  61.  
  62. /* Print a ref. */
  63. private void
  64. debug_print_full_ref(const ref * pref)
  65. {
  66.     uint size = r_size(pref);
  67.     ref nref;
  68.  
  69.     dprintf1("(%x)", r_type_attrs(pref));
  70.     switch (r_type(pref)) {
  71.     case t_array:
  72.         dprintf2("array(%u)0x%lx", size, (ulong) pref->value.refs);
  73.         break;
  74.     case t_astruct:
  75.         goto strct;
  76.     case t_boolean:
  77.         dprintf1("boolean %x", pref->value.boolval);
  78.         break;
  79.     case t_device:
  80.         dprintf1("device 0x%lx", (ulong) pref->value.pdevice);
  81.         break;
  82.     case t_dictionary:
  83.         dprintf3("dict(%u/%u)0x%lx",
  84.              dict_length(pref), dict_maxlength(pref),
  85.              (ulong) pref->value.pdict);
  86.         break;
  87.     case t_file:
  88.         dprintf1("file 0x%lx", (ulong) pref->value.pfile);
  89.         break;
  90.     case t_fontID:
  91.         goto strct;
  92.     case t_integer:
  93.         dprintf1("int %ld", pref->value.intval);
  94.         break;
  95.     case t_mark:
  96.         dprintf("mark");
  97.         break;
  98.     case t_mixedarray:
  99.         dprintf2("mixed packedarray(%u)0x%lx", size,
  100.              (ulong) pref->value.packed);
  101.         break;
  102.     case t_name:
  103.         dprintf2("name(0x%lx#%u)", (ulong) pref->value.pname,
  104.              name_index(pref));
  105.         debug_print_name(pref);
  106.         break;
  107.     case t_null:
  108.         dprintf("null");
  109.         break;
  110.     case t_oparray:
  111.         dprintf2("op_array(%u)0x%lx:", size, (ulong) pref->value.const_refs);
  112.         {
  113.         const op_array_table *opt = op_index_op_array_table(size);
  114.  
  115.         name_index_ref(opt->nx_table[size - opt->base_index], &nref);
  116.         }
  117.         debug_print_name(&nref);
  118.         break;
  119.     case t_operator:
  120.         dprintf1("op(%u", size);
  121.         if (size > 0 && size < op_def_count)    /* just in case */
  122.         dprintf1(":%s", (const char *)(op_index_def(size)->oname + 1));
  123.         dprintf1(")0x%lx", (ulong) pref->value.opproc);
  124.         break;
  125.     case t_real:
  126.         dprintf1("real %f", pref->value.realval);
  127.         break;
  128.     case t_save:
  129.         dprintf1("save %lu", pref->value.saveid);
  130.         break;
  131.     case t_shortarray:
  132.         dprintf2("short packedarray(%u)0x%lx", size,
  133.              (ulong) pref->value.packed);
  134.         break;
  135.     case t_string:
  136.         dprintf2("string(%u)0x%lx", size, (ulong) pref->value.bytes);
  137.         break;
  138.     case t_struct:
  139.       strct:{
  140.         obj_header_t *obj = (obj_header_t *) pref->value.pstruct;
  141.         /* HACK: We know this object was allocated with gsalloc.c. */
  142.         gs_memory_type_ptr_t otype =
  143.             gs_ref_memory_procs.object_type(NULL, obj);
  144.  
  145.         dprintf2("struct %s 0x%lx",
  146.              (r_is_foreign(pref) ? "-foreign-" :
  147.               gs_struct_type_name_string(otype)),
  148.              (ulong) obj);
  149.         }
  150.         break;
  151.     default:
  152.         dprintf1("type 0x%x", r_type(pref));
  153.     }
  154. }
  155. private void
  156. debug_print_packed_ref(const ref_packed * pref)
  157. {
  158.     ushort elt = *pref & packed_value_mask;
  159.     ref nref;
  160.  
  161.     switch (*pref >> r_packed_type_shift) {
  162.     case pt_executable_operator:
  163.         dprintf("<op_name>");
  164.         op_index_ref(elt, &nref);
  165.         debug_print_ref(&nref);
  166.         break;
  167.     case pt_integer:
  168.         dprintf1("<int> %d", (int)elt + packed_min_intval);
  169.         break;
  170.     case pt_literal_name:
  171.         dprintf("<lit_name>");
  172.         goto ptn;
  173.     case pt_executable_name:
  174.         dprintf("<exec_name>");
  175.       ptn:name_index_ref(elt, &nref);
  176.         dprintf2("(0x%lx#%u)", (ulong) nref.value.pname, elt);
  177.         debug_print_name(&nref);
  178.         break;
  179.     default:
  180.         dprintf2("<packed_%d?>0x%x", *pref >> r_packed_type_shift, elt);
  181.     }
  182. }
  183. void
  184. debug_print_ref_packed(const ref_packed *rpp)
  185. {
  186.     if (r_is_packed(rpp))
  187.     debug_print_packed_ref(rpp);
  188.     else
  189.     debug_print_full_ref((const ref *)rpp);
  190.     fflush(dstderr);
  191. }
  192. void
  193. debug_print_ref(const ref * pref)
  194. {
  195.     debug_print_ref_packed((const ref_packed *)pref);
  196. }
  197.  
  198. /* Dump one ref. */
  199. private void print_ref_data(P1(const ref *));
  200. void
  201. debug_dump_one_ref(const ref * p)
  202. {
  203.     uint attrs = r_type_attrs(p);
  204.     uint type = r_type(p);
  205.     static const attr_print_mask apm[] = {
  206.     attr_print_masks,
  207.     {0, 0, 0}
  208.     };
  209.     const attr_print_mask *ap = apm;
  210.  
  211.     if (type >= tx_next_index)
  212.     dprintf1("0x%02x?? ", type);
  213.     else if (type >= t_next_index)
  214.     dprintf("opr* ");
  215.     else
  216.     dprintf1("%s ", type_strings[type]);
  217.     for (; ap->mask; ++ap)
  218.     if ((attrs & ap->mask) == ap->value)
  219.         dputc(ap->print);
  220.     dprintf2(" 0x%04x 0x%08lx", r_size(p), *(const ulong *)&p->value);
  221.     print_ref_data(p);
  222.     fflush(dstderr);
  223. }
  224. private void
  225. print_ref_data(const ref *p)
  226. {
  227. #define BUF_SIZE 30
  228.     byte buf[BUF_SIZE + 1];
  229.     const byte *pchars;
  230.     uint plen;
  231.  
  232.     if (obj_cvs(p, buf, countof(buf) - 1, &plen, &pchars) >= 0 &&
  233.     pchars == buf &&
  234.     ((buf[plen] = 0), strcmp((char *)buf, "--nostringval--"))
  235.     )
  236.     dprintf1(" = %s", (char *)buf);
  237. #undef BUF_SIZE
  238. }
  239.  
  240. /* Dump a region of memory containing refs. */
  241. void
  242. debug_dump_refs(const ref * from, uint size, const char *msg)
  243. {
  244.     const ref *p = from;
  245.     uint count = size;
  246.  
  247.     if (size && msg)
  248.     dprintf2("%s at 0x%lx:\n", msg, (ulong) from);
  249.     while (count--) {
  250.     dprintf2("0x%lx: 0x%04x ", (ulong)p, r_type_attrs(p));
  251.     debug_dump_one_ref(p);
  252.     dputc('\n');
  253.     p++;
  254.     }
  255. }
  256.  
  257. /* Dump a stack. */
  258. void
  259. debug_dump_stack(const ref_stack_t * pstack, const char *msg)
  260. {
  261.     uint i;
  262.     const char *m = msg;
  263.  
  264.     for (i = ref_stack_count(pstack); i != 0;) {
  265.     const ref *p = ref_stack_index(pstack, --i);
  266.  
  267.     if (m) {
  268.         dprintf2("%s at 0x%lx:\n", m, (ulong) pstack);
  269.         m = NULL;
  270.     }
  271.     dprintf2("0x%lx: 0x%02x ", (ulong)p, r_type(p));
  272.     debug_dump_one_ref(p);
  273.     dputc('\n');
  274.     }
  275. }
  276.  
  277. /* Dump an array. */
  278. void
  279. debug_dump_array(const ref * array)
  280. {
  281.     const ref_packed *pp;
  282.     uint type = r_type(array);
  283.     uint len;
  284.  
  285.     switch (type) {
  286.     default:
  287.         dprintf2("%s at 0x%lx isn't an array.\n",
  288.              (type < countof(type_strings) ?
  289.               type_strings[type] : "????"),
  290.              (ulong) array);
  291.         return;
  292.     case t_oparray:
  293.         /* This isn't really an array, but we'd like to see */
  294.         /* its contents anyway. */
  295.         debug_dump_array(array->value.const_refs);
  296.         return;
  297.     case t_array:
  298.     case t_mixedarray:
  299.     case t_shortarray:
  300.         ;
  301.     }
  302.  
  303.     /* This "packed" loop works for all array-types. */
  304.     for (len = r_size(array), pp = array->value.packed;
  305.      len > 0;
  306.      len--, pp = packed_next(pp)) {
  307.     ref temp;
  308.  
  309.     packed_get(pp, &temp);
  310.     if (r_is_packed(pp)) {
  311.         dprintf2("0x%lx* 0x%04x ", (ulong)pp, (uint)*pp);
  312.         print_ref_data(&temp);
  313.     } else {
  314.         dprintf2("0x%lx: 0x%02x ", (ulong)pp, r_type(&temp));
  315.         debug_dump_one_ref(&temp);
  316.     }
  317.     dputc('\n');
  318.     }
  319. }
  320.